package com.example.permissionservice.common.aspect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.permissionservice.common.enums.ResultCode;
import com.example.permissionservice.common.exception.ServiceException;
import com.example.permissionservice.common.redis.RedisService;
import com.example.permissionservice.common.utils.ThreadLocalUtil;
import com.example.permissionservice.mapper.UserRoleMapper;
import com.example.permissionservice.model.UserRole;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 功能描述:
 *
 * @author Lenovo
 * @date 2025/6/21
 */
@Slf4j
@Aspect
@Component
public class UserStatusCheckAspect {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Before(value = "@annotation(com.example.permissionservice.common.aspect.CheckUserStatus)")
    public void before(JoinPoint point){
        Long userId = ThreadLocalUtil.get("userId", Long.class);
        log.info("获取到userId:{}",userId);
        UserRole userRole = userRoleMapper.selectOne(new LambdaQueryWrapper<UserRole>()
                .eq(UserRole::getUserId,userId));
        if (userRole.getRoleId() != 1){
            throw new ServiceException(ResultCode.FAILED_AUTHORITY);
        }
    }
}
